<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 6节课入门Flask框架web开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Flask-WTF表单 | 6节课入门Flask框架web开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter3.html" />
    
    
    <link rel="prev" href="../chapter2/guo-lv-qi.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.3"
        data-chapter-title="Flask-WTF表单"
        data-filepath="chapter2/hong.md"
        data-basepath=".."
        data-revision="Sun Jan 28 2018 01:59:46 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask快速入门课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/flaskjian-jie.html">
            
                
                    <a href="../chapter1/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        为什么要使用WEB框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/flaskyu-django-dui-bi.html">
            
                
                    <a href="../chapter1/flaskyu-django-dui-bi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/flask-an-zhuang-huan-jing.html">
            
                
                    <a href="../chapter1/flask-an-zhuang-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Flask 安装环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/requirements-wen-jian.html">
            
                
                    <a href="../chapter1/requirements-wen-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        requirements 文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/hello-worldcheng-xu.html">
            
                
                    <a href="../chapter1/hello-worldcheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Hello World程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
            
                
                    <a href="../chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        路由定义的基本方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2.html">
            
                
                    <a href="../chapter2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../chapter2/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2模板引擎
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/guo-lv-qi.html">
            
                
                    <a href="../chapter2/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.3" data-path="chapter2/hong.html">
            
                
                    <a href="../chapter2/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3.html">
            
                
                    <a href="../chapter3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/shu-ju-ku-de-she-zhi.html">
            
                
                    <a href="../chapter3/shu-ju-ku-de-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Flask中使用数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
            
                
                    <a href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据库基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        定义模型&amp;创建表&amp;数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        添加数据的表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >6节课入门Flask框架web开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="web&#x8868;&#x5355;">Web&#x8868;&#x5355;</h1>
<p>web&#x8868;&#x5355;&#x662F;web&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x57FA;&#x672C;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x5B83;&#x662F;HTML&#x9875;&#x9762;&#x4E2D;&#x8D1F;&#x8D23;&#x6570;&#x636E;&#x91C7;&#x96C6;&#x7684;&#x90E8;&#x4EF6;&#x3002;&#x8868;&#x5355;&#x6709;&#x4E09;&#x4E2A;&#x90E8;&#x5206;&#x7EC4;&#x6210;&#xFF1A;&#x8868;&#x5355;&#x6807;&#x7B7E;&#x3001;&#x8868;&#x5355;&#x57DF;&#x3001;&#x8868;&#x5355;&#x6309;&#x94AE;&#x3002;&#x8868;&#x5355;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x8F93;&#x5165;&#x6570;&#x636E;&#xFF0C;&#x8D1F;&#x8D23;HTML&#x9875;&#x9762;&#x6570;&#x636E;&#x91C7;&#x96C6;&#xFF0C;&#x901A;&#x8FC7;&#x8868;&#x5355;&#x5C06;&#x7528;&#x6237;&#x8F93;&#x5165;&#x7684;&#x6570;&#x636E;&#x63D0;&#x4EA4;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x3002;</p>
<p>&#x5728;Flask&#x4E2D;&#xFF0C;&#x4E3A;&#x4E86;&#x5904;&#x7406;web&#x8868;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x4F7F;&#x7528;Flask-WTF&#x6269;&#x5C55;&#xFF0C;&#x5B83;&#x5C01;&#x88C5;&#x4E86;WTForms&#xFF0C;&#x5E76;&#x4E14;&#x5B83;&#x6709;&#x9A8C;&#x8BC1;&#x8868;&#x5355;&#x6570;&#x636E;&#x7684;&#x529F;&#x80FD;</p>
<h2 id="wtforms&#x652F;&#x6301;&#x7684;html&#x6807;&#x51C6;&#x5B57;&#x6BB5;">WTForms&#x652F;&#x6301;&#x7684;HTML&#x6807;&#x51C6;&#x5B57;&#x6BB5;</h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5B57;&#x6BB5;&#x5BF9;&#x8C61;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">StringField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;</td>
</tr>
<tr>
<td style="text-align:left">TextAreaField</td>
<td style="text-align:left">&#x591A;&#x884C;&#x6587;&#x672C;&#x5B57;&#x6BB5;</td>
</tr>
<tr>
<td style="text-align:left">PasswordField</td>
<td style="text-align:left">&#x5BC6;&#x7801;&#x6587;&#x672C;&#x5B57;&#x6BB5;</td>
</tr>
<tr>
<td style="text-align:left">HiddenField</td>
<td style="text-align:left">&#x9690;&#x85CF;&#x6587;&#x4EF6;&#x5B57;&#x6BB5;</td>
</tr>
<tr>
<td style="text-align:left">DateField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x503C;&#x4E3A; datetime.date &#x6587;&#x672C;&#x683C;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">DateTimeField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x503C;&#x4E3A; datetime.datetime &#x6587;&#x672C;&#x683C;&#x5F0F;</td>
</tr>
<tr>
<td style="text-align:left">IntegerField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x503C;&#x4E3A;&#x6574;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left">DecimalField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x503C;&#x4E3A;decimal.Decimal</td>
</tr>
<tr>
<td style="text-align:left">FloatField</td>
<td style="text-align:left">&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x503C;&#x4E3A;&#x6D6E;&#x70B9;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left">BooleanField</td>
<td style="text-align:left">&#x590D;&#x9009;&#x6846;&#xFF0C;&#x503C;&#x4E3A;True &#x548C; False</td>
</tr>
<tr>
<td style="text-align:left">RadioField</td>
<td style="text-align:left">&#x4E00;&#x7EC4;&#x5355;&#x9009;&#x6846;</td>
</tr>
<tr>
<td style="text-align:left">SelectField</td>
<td style="text-align:left">&#x4E0B;&#x62C9;&#x5217;&#x8868;</td>
</tr>
<tr>
<td style="text-align:left">SelectMutipleField</td>
<td style="text-align:left">&#x4E0B;&#x62C9;&#x5217;&#x8868;&#xFF0C;&#x53EF;&#x9009;&#x62E9;&#x591A;&#x4E2A;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">FileField</td>
<td style="text-align:left">&#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#x5B57;&#x6BB5;</td>
</tr>
<tr>
<td style="text-align:left">SubmitField</td>
<td style="text-align:left">&#x8868;&#x5355;&#x63D0;&#x4EA4;&#x6309;&#x94AE;</td>
</tr>
<tr>
<td style="text-align:left">FormField</td>
<td style="text-align:left">&#x628A;&#x8868;&#x5355;&#x4F5C;&#x4E3A;&#x5B57;&#x6BB5;&#x5D4C;&#x5165;&#x53E6;&#x4E00;&#x4E2A;&#x8868;&#x5355;</td>
</tr>
<tr>
<td style="text-align:left">FieldList</td>
<td style="text-align:left">&#x4E00;&#x7EC4;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x6BB5;</td>
</tr>
</tbody>
</table>
<h2 id="wtforms&#x5E38;&#x7528;&#x9A8C;&#x8BC1;&#x51FD;&#x6570;">WTForms&#x5E38;&#x7528;&#x9A8C;&#x8BC1;&#x51FD;&#x6570;</h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x9A8C;&#x8BC1;&#x51FD;&#x6570;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">DataRequired</td>
<td style="text-align:left">&#x786E;&#x4FDD;&#x5B57;&#x6BB5;&#x4E2D;&#x6709;&#x6570;&#x636E;</td>
</tr>
<tr>
<td style="text-align:left">EqualTo</td>
<td style="text-align:left">&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#xFF0C;&#x5E38;&#x7528;&#x4E8E;&#x6BD4;&#x8F83;&#x4E24;&#x6B21;&#x5BC6;&#x7801;&#x8F93;&#x5165;</td>
</tr>
<tr>
<td style="text-align:left">Length</td>
<td style="text-align:left">&#x9A8C;&#x8BC1;&#x8F93;&#x5165;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;</td>
</tr>
<tr>
<td style="text-align:left">NumberRange</td>
<td style="text-align:left">&#x9A8C;&#x8BC1;&#x8F93;&#x5165;&#x7684;&#x503C;&#x5728;&#x6570;&#x5B57;&#x8303;&#x56F4;&#x5185;</td>
</tr>
<tr>
<td style="text-align:left">URL</td>
<td style="text-align:left">&#x9A8C;&#x8BC1;URL</td>
</tr>
<tr>
<td style="text-align:left">AnyOf</td>
<td style="text-align:left">&#x9A8C;&#x8BC1;&#x8F93;&#x5165;&#x503C;&#x5728;&#x53EF;&#x9009;&#x5217;&#x8868;&#x4E2D;</td>
</tr>
<tr>
<td style="text-align:left">NoneOf</td>
<td style="text-align:left">&#x9A8C;&#x8BC1;&#x8F93;&#x5165;&#x503C;&#x4E0D;&#x5728;&#x53EF;&#x9009;&#x5217;&#x8868;&#x4E2D;</td>
</tr>
</tbody>
</table>
<p>&#x4F7F;&#x7528;Flask-WTF&#x9700;&#x8981;&#x914D;&#x7F6E;&#x53C2;&#x6570;SECRET_KEY&#x3002;</p>
<p>CSRF_ENABLED&#x662F;&#x4E3A;&#x4E86;CSRF&#xFF08;&#x8DE8;&#x7AD9;&#x8BF7;&#x6C42;&#x4F2A;&#x9020;&#xFF09;&#x4FDD;&#x62A4;&#x3002; SECRET_KEY&#x7528;&#x6765;&#x751F;&#x6210;&#x52A0;&#x5BC6;&#x4EE4;&#x724C;&#xFF0C;&#x5F53;CSRF&#x6FC0;&#x6D3B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8BE5;&#x8BBE;&#x7F6E;&#x4F1A;&#x6839;&#x636E;&#x8BBE;&#x7F6E;&#x7684;&#x5BC6;&#x5319;&#x751F;&#x6210;&#x52A0;&#x5BC6;&#x4EE4;&#x724C;&#x3002;&#x5728;HTML&#x9875;&#x9762;&#x4E2D;&#x76F4;&#x63A5;&#x5199;form&#x8868;&#x5355;&#xFF1A;</p>
<h2 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h2>
<h3 id="&#x4F7F;&#x7528;&#x666E;&#x901A;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x8868;&#x5355;">&#x4F7F;&#x7528;&#x666E;&#x901A;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x8868;&#x5355;</h3>
<h4 id="&#x5728;html&#x9875;&#x9762;&#x4E2D;&#x76F4;&#x63A5;&#x5199;form&#x8868;&#x5355;&#xFF1A;">&#x5728;HTML&#x9875;&#x9762;&#x4E2D;&#x76F4;&#x63A5;&#x5199;form&#x8868;&#x5355;&#xFF1A;</h4>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">label</span>&gt;</span>&#x7528;&#x6237;&#x540D;:<span class="hljs-tag">&lt;/<span class="hljs-title">label</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;username&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">label</span>&gt;</span>&#x5BC6;&#x7801;:<span class="hljs-tag">&lt;/<span class="hljs-title">label</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;password&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;password&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">label</span>&gt;</span>&#x786E;&#x8BA4;&#x5BC6;&#x7801;:<span class="hljs-tag">&lt;/<span class="hljs-title">label</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;password&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;password2&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x63D0;&#x4EA4;&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    {% for message in get_flashed_messages() %}
        {{ message }}
    {% endfor %}
<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
<h4 id="&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x83B7;&#x53D6;&#x8868;&#x5355;&#x6570;&#x636E;&#xFF1A;">&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x83B7;&#x53D6;&#x8868;&#x5355;&#x6570;&#x636E;&#xFF1A;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,render_template,request

app.secret_key = <span class="hljs-string">&apos;heima&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/&apos;, methods=[&apos;GET&apos;, &apos;POST&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_world</span><span class="hljs-params">()</span>:</span>

    <span class="hljs-comment"># 1. &#x5224;&#x65AD;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x662F;post</span>
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span>:

        <span class="hljs-comment"># 2. &#x83B7;&#x53D6;&#x53C2;&#x6570;, &#x5E76;&#x6548;&#x9A8C;&#x53C2;&#x6570;&#x5B8C;&#x6574;&#x6027;, &#x5982;&#x679C;&#x6709;&#x95EE;&#x9898;&#x5C31;&#x8FDB;&#x884C;flash</span>
        username = request.form.get(<span class="hljs-string">&apos;username&apos;</span>)
        password = request.form.get(<span class="hljs-string">&apos;password&apos;</span>)
        password2 = request.form.get(<span class="hljs-string">&apos;password2&apos;</span>)
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> all([username, password, password2]):
            flash(<span class="hljs-string">&apos;params error&apos;</span>)

        <span class="hljs-comment"># 3. &#x6548;&#x9A8C;&#x5BC6;&#x7801;</span>
        <span class="hljs-keyword">elif</span> password != password2:
            flash(<span class="hljs-string">&apos;password error&apos;</span>)

        <span class="hljs-comment"># 4. &#x6CA1;&#x6709;&#x95EE;&#x9898;&#x5C31;&#x8FD4;&#x56DE;&apos;success&apos;</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">print</span> username
            <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;success&apos;</span>

    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;wtf.html&apos;</span>)
</code></pre>
<h3 id="&#x4F7F;&#x7528;flaskwtf&#x5B9E;&#x73B0;&#x8868;&#x5355;">&#x4F7F;&#x7528;Flask-WTF&#x5B9E;&#x73B0;&#x8868;&#x5355;</h3>
<h4 id="&#x6A21;&#x677F;&#x9875;&#x9762;&#xFF1A;">&#x6A21;&#x677F;&#x9875;&#x9762;&#xFF1A;</h4>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>
    {#&#x8BBE;&#x7F6E;csrf_token#}
    {{ form.csrf_token() }}
    {{ form.username.label }}{{ form.username }}<span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    {{ form.password.label }}{{ form.password }}<span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    {{ form.password2.label }}{{ form.password2 }}<span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
    {{ form.input }}<span class="hljs-tag">&lt;<span class="hljs-title">br</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
</code></pre>
<h4 id="&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF1A;">&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF1A; </h4>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template, request, flash

<span class="hljs-comment">#&#x5BFC;&#x5165;wtf&#x6269;&#x5C55;&#x7684;&#x8868;&#x5355;&#x7C7B;</span>
<span class="hljs-keyword">from</span> flask_wtf <span class="hljs-keyword">import</span> FlaskForm

<span class="hljs-comment">#&#x5BFC;&#x5165;&#x81EA;&#x5B9A;&#x4E49;&#x8868;&#x5355;&#x9700;&#x8981;&#x7684;&#x5B57;&#x6BB5;</span>
<span class="hljs-keyword">from</span> wtforms <span class="hljs-keyword">import</span> SubmitField,StringField,PasswordField

<span class="hljs-comment">#&#x5BFC;&#x5165;wtf&#x6269;&#x5C55;&#x63D0;&#x4F9B;&#x7684;&#x8868;&#x5355;&#x9A8C;&#x8BC1;&#x5668;</span>
<span class="hljs-keyword">from</span> wtforms.validators <span class="hljs-keyword">import</span> DataRequired,EqualTo

<span class="hljs-comment"># &#x89E3;&#x51B3;&#x7F16;&#x7801;&#x95EE;&#x9898;</span>
<span class="hljs-keyword">import</span> sys
reload(sys)
sys.setdefaultencoding(<span class="hljs-string">&quot;utf-8&quot;</span>)

app = Flask(__name__)
app.config[<span class="hljs-string">&apos;SECRET_KEY&apos;</span>]=<span class="hljs-string">&apos;heima&apos;</span>

<span class="hljs-comment">#&#x81EA;&#x5B9A;&#x4E49;&#x8868;&#x5355;&#x7C7B;&#xFF0C;&#x6587;&#x672C;&#x5B57;&#x6BB5;&#x3001;&#x5BC6;&#x7801;&#x5B57;&#x6BB5;&#x3001;&#x63D0;&#x4EA4;&#x6309;&#x94AE;</span>
<span class="hljs-comment"># &#x9700;&#x8981;&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x8868;&#x5355;&#x7C7B;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RegisterForm</span><span class="hljs-params">(FlaskForm)</span>:</span>
    username = StringField(<span class="hljs-string">&apos;&#x7528;&#x6237;&#x540D;:&apos;</span>, validators=[DataRequired()]})
    password = PasswordField(<span class="hljs-string">&apos;&#x5BC6;&#x7801;:&apos;</span>, validators=[DataRequired()])
    password2 = PasswordField(<span class="hljs-string">&apos;&#x786E;&#x8BA4;&#x5BC6;&#x7801;:&apos;</span>, validators=[DataRequired(), EqualTo(<span class="hljs-string">&apos;password&apos;</span>, <span class="hljs-string">&apos;&#x5BC6;&#x7801;&#x8F93;&#x5165;&#x4E0D;&#x4E00;&#x81F4;&apos;</span>)])
    input = SubmitField(<span class="hljs-string">&apos;&#x63D0;&#x4EA4;&apos;</span>)

<span class="hljs-comment">#&#x5B9A;&#x4E49;&#x6839;&#x8DEF;&#x7531;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF0C;&#x751F;&#x6210;&#x8868;&#x5355;&#x5BF9;&#x8C61;&#xFF0C;&#x83B7;&#x53D6;&#x8868;&#x5355;&#x6570;&#x636E;&#xFF0C;&#x8FDB;&#x884C;&#x8868;&#x5355;&#x6570;&#x636E;&#x9A8C;&#x8BC1;</span>
<span class="hljs-decorator">@app.route(&apos;/form&apos;, methods=[&apos;GET&apos;, &apos;POST&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">form</span><span class="hljs-params">()</span>:</span>
    register_form = RegisterForm()

    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span>:
        <span class="hljs-comment"># &#x8C03;&#x7528;validate_on_submit&#x65B9;&#x6CD5;, &#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x6027;&#x6267;&#x884C;&#x5B8C;&#x6240;&#x6709;&#x7684;&#x9A8C;&#x8BC1;&#x51FD;&#x6570;&#x7684;&#x903B;&#x8F91;</span>
        <span class="hljs-keyword">if</span> register_form.validate_on_submit():
            <span class="hljs-comment"># &#x8FDB;&#x5165;&#x8FD9;&#x91CC;&#x5C31;&#x8868;&#x793A;&#x6240;&#x6709;&#x7684;&#x903B;&#x8F91;&#x90FD;&#x9A8C;&#x8BC1;&#x6210;&#x529F;</span>
            username = request.form.get(<span class="hljs-string">&apos;username&apos;</span>)
            password = request.form.get(<span class="hljs-string">&apos;password&apos;</span>)
            password2 = request.form.get(<span class="hljs-string">&apos;password2&apos;</span>)
            <span class="hljs-keyword">print</span> username
            <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;success&apos;</span>

        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment">#message = register_form.errors.get(&apos;password2&apos;)[0]</span>
            <span class="hljs-comment">#flash(message)</span>
            flash(<span class="hljs-string">&apos;&#x53C2;&#x6570;&#x6709;&#x8BEF;&apos;</span>)

    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">&apos;wtf.html&apos;</span>, form=register_form)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter2/guo-lv-qi.html" class="navigation navigation-prev " aria-label="Previous page: 过滤器"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3.html" class="navigation navigation-next " aria-label="Next page: 数据库"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 6节课入门Flask框架web开发 -->
</html>
